package com.gyp.utils;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import java.net.URL;

public class AudioUtils {

    public static long getAudioDurationFromUrl(String audioUrl) {
        try {
            // 创建音频输入流
            AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new URL(audioUrl));

            // 获取音频剪辑
            Clip clip = AudioSystem.getClip();

            // 加载音频
            clip.open(audioInputStream);

            // 获取音频时长（以毫秒为单位）
            long duration = clip.getMicrosecondLength() / ( 1000 * 1000);

            // 关闭音频剪辑
            clip.close();

            return duration;
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }



}
